﻿using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;

namespace Leo.Common.Logger
{
    public class Logs<T> : ILogs<T>
    {
        private readonly ILoggerFactory _loggerFactory;
        private readonly ILogger<T> _logger;
        public Logs()
        {
            NLogConfigurator.Init();
            _loggerFactory = LoggerFactory.Create(builder => { builder.AddNLog(); });
            _logger = _loggerFactory.CreateLogger<T>();
        }

        public void Debug(string message)
        {
            _logger.LogDebug(message);
        }

        public void Info(string message)
        {
            _logger.LogInformation(message);
        }

        public void Warn(string message)
        {
            _logger.LogWarning(message);
        }

        public void Error(string message, Exception? ex = null)
        {
            _logger.LogError(message, ex);
        }
    }

    public class Logs : ILogs
    {
        private readonly ILoggerFactory _loggerFactory;

        public Logs()
        {
            NLogConfigurator.Init();
            _loggerFactory = LoggerFactory.Create(builder => { builder.AddNLog(); });
        }

        public ILogger<T> CreateLogger<T>()
        {
            return _loggerFactory.CreateLogger<T>();
        }

        /// <summary>
        /// 调试日志
        /// </summary>
        /// <typeparam name="T">当前类名</typeparam>
        /// <param name="message">消息</param>
        public void Debug<T>(string message)
        {
            CreateLogger<T>().LogDebug(message);
        }

        /// <summary>
        /// 消息日志
        /// </summary>
        /// <typeparam name="T">当前类名</typeparam>
        /// <param name="message">消息</param>
        public void Info<T>(string message) => CreateLogger<T>().LogInformation(message);
        /// <summary>
        /// 警告日志
        /// </summary>
        /// <typeparam name="T">当前类名</typeparam>
        /// <param name="message">消息</param>
        public void Warn<T>(string message) => CreateLogger<T>().LogWarning(message);
        /// <summary>
        /// 错误日志
        /// </summary>
        /// <typeparam name="T">当前类名</typeparam>
        /// <param name="message">消息</param>
        public void Error<T>(string message, Exception? ex = null) => CreateLogger<T>().LogError(message, ex);
        /// <summary>
        /// 严重错误 日志
        /// </summary>
        /// <typeparam name="T">当前类名</typeparam>
        /// <param name="message">消息</param>
        public void Critical<T>(string message, Exception? ex = null) => CreateLogger<T>().LogCritical(message, ex);


        //public void Debug(string message) => LogManager.GetCurrentClassLogger().Debug(message);
        //public void Info(string message) => LogManager.GetCurrentClassLogger().Info(message);
        //public void Warn(string message) => LogManager.GetCurrentClassLogger().Warn(message);
        //public void Error(string message, Exception? ex = null) => LogManager.GetCurrentClassLogger().Error(ex, message);
        //public void Critical(string message, Exception? ex = null) => LogManager.GetCurrentClassLogger().Fatal(ex, message);
    }

}
